<template>
  <div class="login-container">
    <div class="left" />
    <div class="right">
      <h1>登录</h1>
      <div class="from-container">
        <el-form ref="formRef" :model="form" :rules="rules">
          <el-form-item prop="mobile">
            <el-input
              ref="inputRef"
              v-model="form.mobile"
              prefix-icon="el-icon-eleme"
              clearable
              placeholder="请输入手机号"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              prefix-icon="el-icon-loading"
              placeholder="请输入密码"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="checked"> 用户平台使用协议 </el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button class="from-btn" type="primary" @click="submit">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
// import request from '@/utils/request'
export default {
  name: 'LoginCom',
  data () {
    return {
      form: {
        mobile: process.env.NODE_ENV === "development" ? '13800000002' : '',
        password: process.env.NODE_ENV === "development" ? 'hm#qd@23!' : ''
      },
      checked: true,
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[345678][0-9]{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
  },
  created () {
  },
  methods: {

    async submit () {
      if (!this.checked) {
        this.$message.error('请同意勾选协议')
        return
      }
      //表单兜底校验
      //1.官方
      const that = this//不用箭头函数的时候要在外层定义一个this
      this.$refs.formRef.validate(async function (valid) {
        // that.$store.commit('user/setToken', 456)
        // that.$store.dispatch('user/setTokenActions', that.form)

        if (valid) {
          await that.$store.dispatch('user/setTokenActions', that.form)
          // 跳转主页
          that.$router.push('/')
          // alert('submit!');
          // request.post('/sys/login', that.form).then(res => {
          //   console.log(3, res.data)
          //   that.$store.dispatch('user/setTokenActions', res.data)

          // })
        }
      });

      //2.promise then catch的写法
      // this.$refs.formRef.validate().then(res => {
      //   console.log(res, 999)
      // }).catch(err => {
      //   console.log(err)

      // })

      //3.try catch 写法
      // try {
      //   const res = await this.$refs.formRef.validate()
      //   console.log(1111, res)

      // } catch (error) {
      //   console.log(error)

      // }
    },

  }
}
</script>
<style lang='scss'  scoped>
.login-container {
  // background-color: skyblue;
  width: 100%;
  height: 100vh;
  display: flex;
  .left {
    flex: 3 1 0%;
    background: url(../../assets/common/login_back.png);
    background-size: cover;
    padding: 0 100px;
    background-position: center;
    background-repeat: no-repeat;
    border-top-right-radius: 100px;
  }

  .right {
    flex: 2 1 0%;
    // background-color: lightcyan;
    flex-direction: column;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .from-container {
    width: 70%;
  }
  .el-button.from-btn {
    width: 100%;
  }
}
</style>
